package com.zxkj.commonpatrol.adapt;

import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.RequiresApi;
import androidx.fragment.app.FragmentManager;

import com.SuperKotlin.pictureviewer.ImagePagerActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.czy.basecommon.utils.base.BaseRepository;
import com.czy.basecommon.utils.constant.ItrackConstant;
import com.czy.basecommon.utils.tools.ActivityTool;
import com.czy.basecommon.utils.tools.GsonUtil;
import com.czy.basecommon.utils.tools.MyImageLoader;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.bottomsheet.BottomSheet;
import com.xuexiang.xui.widget.toast.XToast;
import com.zxkj.commonpatrol.R;
import com.zxkj.commonpatrol.model.entity.itemPicEntity;
import com.zxkj.commonpatrol.model.entity.itemVioceEntity;
import com.zxkj.commonpatrol.model.livedata.DiffBean;
import com.zxkj.commonpatrol.model.response.MediaInfoResponse;
import com.zxkj.commonpatrol.ui.PlayerVioceActivity;
import com.zxkj.commonpatrol.ui.ShowMediaActivity;
import com.zxkj.commonpatrol.utils.MediaManager;
import com.zxkj.commonpatrol.view.VioceDialog;
import com.zxkj.commonpatrol.viewmodel.ShowMediaDetailViewModel;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FzTestFinalAdapt extends BaseQuickAdapter<MediaInfoResponse, BaseViewHolder> {


    private FragmentManager FragManager;
    private ShowMediaDetailViewModel viewModel;
    private List<BottomSheet.BottomListSheetBuilder.BottomSheetListItemData> mItemsVioce;
    private List<MediaInfoResponse> datas = new ArrayList<>();
    private List<MediaInfoResponse> Vicoedatas = new ArrayList<>();
    private MiniLoadingDialog loadingDialog;

    public FzTestFinalAdapt(ShowMediaDetailViewModel viewModel, int layoutResId, @Nullable List<MediaInfoResponse> mediaInfoResponses, MiniLoadingDialog loadingDialog) {
        super(layoutResId, mediaInfoResponses);
        FragManager = ActivityTool.getInstance().getLastActivity().getSupportFragmentManager();
        this.viewModel = viewModel;
    }


    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void convert(@NotNull BaseViewHolder helper, @Nullable MediaInfoResponse data) {

        ImageView iv_Pic = helper.itemView.findViewById(R.id.iv1);
        ImageView iv_Vioce = helper.itemView.findViewById(R.id.vioce);
        ImageView iv_Video = helper.itemView.findViewById(R.id.video);
        Log.e("刷新了","刷新了");
        if (data.getFristPath()!=null){
                MyImageLoader.load(getContext(), data.getFristPath(), iv_Pic, R.drawable.nodatas);
        }
//        iv_Pic.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//
//                return false;
//            }
//        });
        iv_Pic.setOnClickListener(v->{
            if (data.getPhoto()!=null&&data.getPhoto().size()>0){

                if (data.getPhotoPtahs()!=null&&data.getPhotoPtahs().size()>0){
                    //已经下载
                    ArrayList<String> photoPtahs = data.getPhotoPtahs();
                    com.SuperKotlin.pictureviewer.PictureConfig config = new com.SuperKotlin.pictureviewer.PictureConfig.Builder()
                            .setListData(photoPtahs)    //图片数据List<String> list
                            .setPosition(0)    //图片下标（从第position张图片开始浏览）
                            .setIsShowNumber(true)//是否显示数字下标
                            .needDownload(false)    //是否支持图片下载
                            .setPlacrHolder(R.drawable.nodatas)    //占位符图片（图片加载完成前显示的资源图片，来源drawable或者mipmap）
                            .build();
                    ImagePagerActivity.startActivity(getContext(), config);
                }else{
                    if (loadingDialog==null){
                         loadingDialog = new MiniLoadingDialog(getContext(), "加載大圖數據中...");

                    }
                    loadingDialog.show();
                    //去下载
                    List<MediaInfoResponse.MediaPhoto> photo = data.getPhoto();
                    Map<Integer, MediaInfoResponse.MediaPhoto> maps=new HashMap<>();
                    for (int i = 0; i < photo.size(); i++) {
                        maps.put(i,photo.get(i));
                    }
                    viewModel.mRepository.DownPo(maps, new BaseRepository.MyBaseDataCallBack<Map<Integer, String>>() {
                        @Override
                        public void onDataSuccessCallBack(Map<Integer, String> integerStringMap) {
                            String s = GsonUtil.getGson().toJson(getData());
                            List<MediaInfoResponse> CopyData = new Gson().fromJson(s, new TypeToken<List<MediaInfoResponse>>(){}.getType());
                            ArrayList<String> data=new ArrayList<>();
                            for (Map.Entry<Integer, String> entry : integerStringMap.entrySet()) {
                                data.add(entry.getValue());
                                CopyData.get(helper.getLayoutPosition()).setPhotoPtahs(data);
                            }
                                    //子线程  强行转Ui线程
                            ((ShowMediaActivity)getContext()).runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    loadingDialog.dismiss();
                                    com.SuperKotlin.pictureviewer.PictureConfig config = new com.SuperKotlin.pictureviewer.PictureConfig.Builder()
                                            .setListData(data)    //图片数据List<String> list
                                            .setPosition(0)    //图片下标（从第position张图片开始浏览）
                                            .setIsShowNumber(true)//是否显示数字下标
                                            .needDownload(false)    //是否支持图片下载
                                            .setPlacrHolder(R.drawable.nodatas)    //占位符图片（图片加载完成前显示的资源图片，来源drawable或者mipmap）
                                            .build();
                                    ImagePagerActivity.startActivity(getContext(), config);
                                    DiffBean diffBean = new DiffBean();
                                    diffBean.setCopyData(CopyData);
                                    LiveEventBus.get("dif").post(diffBean);
                                }
                            });


                        }

                        @Override
                        public void onDataFailCallBack(Map<Integer, String> integerStringMap, Map<Integer, String> t1) {

                        }
                    });

                }
            }

        });
        if (data.getVoice()==null||data.getVoice().size()==0){
            iv_Vioce.setImageResource(R.drawable.vioce_h);
        }else{
            iv_Vioce.setImageResource(R.drawable.voice_defualt);
        }
        if (data.getVedio()==null||data.getVedio().size()==0){
            iv_Video.setImageResource(R.drawable.video_h);
        }else{
            iv_Video.setImageResource(R.drawable.video_defualt);
        }

          iv_Vioce.setOnClickListener(v->{

              if (data.getVoice()!=null&&data.getVoice().size()>0){
                  //去paly

                  List<MediaInfoResponse.MediaVoice> voice = data.getVoice();
                  String s = GsonUtil.getGson().toJson(voice);
                  Intent  intent  =new Intent(getContext(), PlayerVioceActivity.class);
                  intent.putExtra("data",s);
                 getContext().startActivity(intent);
              }


          });



        }
    }

